<?php
/**
  * @todo 文件上传
  * @copyright gaoxiang 2009
  * @author gaoxiang
  * @createdate 2009.04.21
  * @license 本程序遵照Creative Commons 署名-非商业性使用-相同方式共享 2.5
  * 中国大陆协议，该协议的中文版本http://creativecommons.org/licenses/by-nc-sa/2.5/cn/
  **/ 
  $check_online=1;
  require_once(dirname(dirname(__FILE__)) . '/require/load.php');
  session_start();
  ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>金龙集团铝管事业部数据系统</title>
	<meta name="description" content="金龙集团铝管事业部数据系统" />
	<link rel="stylesheet" type="text/css" href="<?php echo $database->arr_config_r[0]; ?>css/upload.css" media="all" />
	<!--[if lte IE 6]>
<style type="text/css">
/* styling specific to Internet Explorer IE5.5 and IE6. Yet to see if IE7 handles li:hover */

/* Get rid of any default table style */
.menu table {
border-collapse:collapse;
margin:-1px;
}
/* ignore the link used by 'other browsers' */
.menu ul li a.hide, .menu ul li a:visited.hide {
display:none;
}
/*隐藏一个东西*/
.menu .hide {display:none;}
/* set the background and foreground color of the main menu link on hover */
.menu ul li a:hover {
color:#fff;
background:#000;
}
/* make the sub menu ul visible and position it beneath the main menu list item */
.menu ul li a:hover ul {
display:block;position:absolute;top:23px;left:0;width:100%;border-top:1px solid #fff;
}
/* style the background and foreground color of the submenu links */
.menu ul li a:hover ul li a {
display:block;background:#757575;color:#fff;height:auto;line-height:16px;padding:5px 16px 5px 16px;width:120px;
}
/* style the background and forground colors of the links on hover */
.menu ul li a:hover ul li a:hover {
background:#202D3D;color:#fff;
}
</style>
<![endif]-->	
 </head>
 <body id="body" onload="startclock()">
	<div id="page">
	<div id="logo"><img src="<?php echo $database->arr_config_r[0]; ?>images/logo2.jpg" alt="金龙集团铝管事业部数据系统" /><span><h1> 金龙集团铝管事业部数据系统</h1></span>
	<div class="menucontainer">
	<div class="menu">
	<ul>
		<li>
            <a href="<?php echo $database->arr_config_r[0]; ?>" target="_self">首页</a>  
        </li>
        <li>
            <a href="#" target="_self" style="border-style:none;">生产 
				<table><tr><td><ul>
						<li><a href="<?php echo $database->arr_config_r[0] . "action/produce.php?action=prequery";?>" target="_self">生产记录查询</a></li>
						<?php if(check_rights(2,0)){//如果用户有生产权限
						echo '<li><a href="' . $database->arr_config_r[0] . 'action/produce.php?action=product_edit_input" target="_self">生产记录修改</a></li>
						<li><a href="' . $database->arr_config_r[0] . 'action/produce.php?action=predit" target="_self">生产记录输入</a></li>
						<li><a href="' . $database->arr_config_r[0] . 'action/leadupload.php?action=product" target="_self">生产记录导入</a></li>';}
						if(check_rights(1,0)){//如果用户有技术权限
							$inspection=$database->fetch_inspection(1);
							if($inspection)
								echo '<li><a href="'. $database->arr_config_r[0] . 'action/produce.php?action=inspection">待检品(' . $inspection . ')</a></li>';
						}
						?>
				</ul></td></tr></table></a>
		</li>
        <li>
            <a href="#" target="_self">原材料  
            	<table><tr><td><ul>
                        <li><a href="<?php echo $database->arr_config_r[0] . "action/rog.php?action=prequery";?>" target="_self">原材料记录查询</a></li>
                        <?php if(check_rights(4,0)){//如果用户有原材料权限
							echo '<li><a href="' . $database->arr_config_r[0] . 'action/rog.php?action=prequery_edit_input" target="_self">原材料记录修改</a></li>
						<li><a href="' . $database->arr_config_r[0] . 'action/rog.php?action=preinput" target="_self">原材料记录输入</a></li>
						<li><a href="' . $database->arr_config_r[0] . 'action/leadupload.php?action=rog" target="_self">原材料记录导入</a></li>';}?>
             	</ul></td></tr></table></a> 
        </li>
				<li>
            <a href="#" target="_self">实验室
            	<table><tr><td><ul>
                        <li><a href="<?php echo $database->arr_config_r[0] . "index.php";?>" target="_self">占位</a></li>
              </ul></td></tr></table></a> 
        </li>
				<li>
            <a href="#" target="_self">其他
            	<table><tr><td><ul>
                        <li><a href="<?php echo $database->arr_config_r[0] . "tools/index.php";?>" target="_self">常用工具及信息</a></li>
              </ul></td></tr></table></a> 
        </li>
        <?php if (isset($_SESSION["userid"]) && isset($_SESSION["rights"])){?>			
			<li class="hide" style="margin-left: 315px;"><a href="<?php echo $database->arr_config_r[0] . "admin/login.php?log=logout"; ?>">注销</a></li>
			<!--[if lte IE 6]>
				<li style="margin-left: 315px;"><a href="<?php echo $database->arr_config_r[0] . "admin/login.php?log=logout"; ?>">注销</a></li>
			<![endif]-->
			<li><a href="<?php echo $database->arr_config_r[0] . "admin/login.php?log=passalter"; ?>">修改密码</a></li>
			<?php }else{ ?>
	<li class="hide" style="margin-left: 410px;"><a href="<?php echo $database->arr_config_r[0] . "admin/login.php?log=login"; ?>">登陆</a></li>
	<!--[if lte IE 6]>
		<li style="margin-left: 380px;"><a href="<?php echo $database->arr_config_r[0] . "admin/login.php?log=login"; ?>">登陆</a></li>
	<![endif]-->
<?php }?>
	<li><a href="<?php echo $database->arr_config_r[0] . "doc/help.php"; ?>">帮助</a></li>
	</ul>
	</div></div></div>	<div style="clear: both;">&nbsp;</div>
 <?php check_user();
 if($_GET['action']=='product')
 	$right_1=2;
 elseif($_GET['action']=='rog')
 	$right_1=4;
 else{
 	set_refer();
 	login_echo($database->arr_config_r[0],"来源不正确");
 }
 check_rights($right_1);
 
 ?>
<form id="uploadForm" action="leadin.php?action=<?php echo $_GET['action'];?>">
  <table border="0" cellspacing="1" class="fu_list">
    <thead>
      <tr>
        <td colspan="2"><b><?php if($_GET['action']=='product') echo "生产"; elseif($_GET['action']=='rog') echo "原材料";?>记录导入</b></td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="right" width="15%" style="line-height:35px;">添加文件：</td>
        <td><a href="javascript:void(0);" class="files" id="idFile"></a> <img id="idProcess" style="display:none;" src="<?php echo $database->arr_config_r[0]; ?>images/loading_up.gif" /></td>
      </tr>
      <tr>
        <td colspan="2"><table border="0" cellspacing="0">
            <thead>
              <tr>
                <td>文件路径</td>
                <td width="100"></td>
              </tr>
            </thead>
            <tbody id="idFileList">
            </tbody>
          </table></td>
      </tr>
      <tr>
        <td colspan="2" style="color:gray">最多可同时上传 <b id="idLimit"></b> 个文件，每个文件限制为<b>10M</b>，只允许上传 <b id="idExt"></b> 文件。导入前请查看<a href="<?php if($_GET['action']=='product') echo $database->arr_config_r[0] . "doc/help-leadinp.php"; elseif($_GET['action']=='rog') echo $database->arr_config_r[0] . "doc/help-leadinr.php"; ?>">说明</a>或者<a href="<?php if($_GET['action']=='product') echo $database->arr_config_r[0] . "action/checkxls.php?action=upload&type=produce"; elseif($_GET['action']=='rog') echo $database->arr_config_r[0] . "action/checkxls.php?action=upload&type=rog"; ?>">检查文件</a></td>
      </tr>
      <tr>
        <td colspan="2" align="center" id="idMsg"><button type="button" id="idBtnupload" disabled="disabled" >开始导入</button>
          &nbsp;&nbsp;&nbsp;
          <button type="button" id="idBtndel" disabled="disabled" >全部取消</button>
        </td>
      </tr>
    </tbody>
  </table>
</form>
<script type="text/javascript">

var isIE = (document.all) ? true : false;

var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
}

var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	}
}

var Each = function(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

//文件上传类
var FileUpload = Class.create();
FileUpload.prototype = {
  //表单对象，文件控件存放空间
  initialize: function(form, folder, options) {
	
	this.Form = $(form);//表单
	this.Folder = $(folder);//文件控件存放空间
	this.Files = [];//文件集合
	
	this.SetOptions(options);
	
	this.FileName = this.options.FileName;
	this._FrameName = this.options.FrameName;
	this.Limit = this.options.Limit;
	this.Distinct = !!this.options.Distinct;
	this.ExtIn = this.options.ExtIn;
	this.ExtOut = this.options.ExtOut;
	
	this.onIniFile = this.options.onIniFile;
	this.onEmpty = this.options.onEmpty;
	this.onNotExtIn = this.options.onNotExtIn;
	this.onExtOut = this.options.onExtOut;
	this.onLimite = this.options.onLimite;
	this.onSame = this.options.onSame;
	this.onFail = this.options.onFail;
	this.onIni = this.options.onIni;
	
	if(!this._FrameName){
		//为每个实例创建不同的iframe
		this._FrameName = "uploadFrame_" + Math.floor(Math.random() * 1000);
		//ie不能修改iframe的name
		var oFrame = isIE ? document.createElement("<iframe name=\"" + this._FrameName + "\">") : document.createElement("iframe");
		//为ff设置name
		oFrame.name = this._FrameName;
		oFrame.style.display = "none";
		//在ie文档未加载完用appendChild会报错
		document.body.insertBefore(oFrame, document.body.childNodes[0]);
	}
	
	//设置form属性，关键是target要指向iframe
	this.Form.target = this._FrameName;
	this.Form.method = "post";
	//注意ie的form没有enctype属性，要用encoding
	this.Form.encoding = "multipart/form-data";
	

	//整理一次
	this.Ini();
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
		FileName:	"Files[]",//文件上传控件的name，配合后台使用
		FrameName:	"",//iframe的name，要自定义iframe的话这里设置name
		onIniFile:	function(){},//整理文件时执行(其中参数是file对象)
		onEmpty:	function(){},//文件空值时执行
		Limit:		10,//文件数限制，0为不限制
		onLimite:	function(){},//超过文件数限制时执行
		Distinct:	true,//是否不允许相同文件
		onSame:		function(){},//有相同文件时执行
		ExtIn:		["xls"],//允许后缀名
		onNotExtIn:	function(){},//不是允许后缀名时执行
		ExtOut:		[],//禁止后缀名，当设置了ExtIn则ExtOut无效
		onExtOut:	function(){},//是禁止后缀名时执行
		onFail:		function(){},//文件不通过检测时执行(其中参数是file对象)
		onIni:		function(){}//重置时执行
    };
    Extend(this.options, options || {});
  },
  //整理空间
  Ini: function() {
	//整理文件集合
	this.Files = [];
	//整理文件空间，把有值的file放入文件集合
	Each(this.Folder.getElementsByTagName("input"), Bind(this, function(o){
		if(o.type == "file"){ o.value && this.Files.push(o); this.onIniFile(o); }
	}))
	//插入一个新的file
	var file = document.createElement("input");
	file.name = this.FileName; file.type = "file"; file.onchange = Bind(this, function(){ this.Check(file); this.Ini(); });
	this.Folder.appendChild(file);
	//执行附加程序
	this.onIni();
  },
  //检测file对象
  Check: function(file) {
	//检测变量
	var bCheck = true;
	//空值、文件数限制、后缀名、相同文件检测
	if(!file.value){
		bCheck = false; this.onEmpty();
	} else if(this.Limit && this.Files.length >= this.Limit){
		bCheck = false; this.onLimite();
	} else if(!!this.ExtIn.length && !RegExp("\.(" + this.ExtIn.join("|") + ")$", "i").test(file.value)){
		//检测是否允许后缀名
		bCheck = false; this.onNotExtIn();
	} else if(!!this.ExtOut.length && RegExp("\.(" + this.ExtOut.join("|") + ")$", "i").test(file.value)) {
		//检测是否禁止后缀名
		bCheck = false; this.onExtOut();
	} else if(!!this.Distinct) {
		Each(this.Files, function(o){ if(o.value == file.value){ bCheck = false; } })
		if(!bCheck){ this.onSame(); }
	}
	//没有通过检测
	!bCheck && this.onFail(file);
  },
  //删除指定file
  Delete: function(file) {
	//移除指定file
	this.Folder.removeChild(file); this.Ini();
  },
  //删除全部file
  Clear: function() {
	//清空文件空间
	Each(this.Files, Bind(this, function(o){ this.Folder.removeChild(o); })); this.Ini();
  }
}

var fu = new FileUpload("uploadForm", "idFile", { 
	onIniFile: function(file){ file.value ? file.style.display = "none" : this.Folder.removeChild(file); },
	onEmpty: function(){ alert("请选择一个文件"); },
	onLimite: function(){ alert("超过上传限制"); },
	onSame: function(){ alert("已经有相同文件"); },
	onNotExtIn:	function(){ alert("只允许上传" + this.ExtIn.join("，") + "文件"); },
	onFail: function(file){ this.Folder.removeChild(file); },
	onIni: function(){
		//显示文件列表
		var arrRows = [];
		if(this.Files.length){
			var oThis = this;
			Each(this.Files, function(o){
				var a = document.createElement("a"); a.innerHTML = "取消"; a.href = "javascript:void(0);";
				a.onclick = function(){ oThis.Delete(o); return false; };
				arrRows.push([o.value, a]);
			});
		} else { arrRows.push(["<font color='gray'>没有添加文件</font>", "&nbsp;"]); }
		AddList(arrRows);
		//设置按钮
		$("idBtnupload").disabled = $("idBtndel").disabled = this.Files.length <= 0;
	}
});

$("idBtnupload").onclick = function(){
	//显示文件列表
	var arrRows = [];
	Each(fu.Files, function(o){ arrRows.push([o.value, "&nbsp;"]); });
	AddList(arrRows);
	
	fu.Folder.style.display = "none";
	$("idProcess").style.display = "";
	$("idMsg").innerHTML = "正在导入，请稍候……<br />有可能因为网络问题，出现程序长时间无响应，请点击“<a href='' onClick='window.location.reload(true)'><font color='#FF0000'>取消</font></a>”重新上传文件";
	
	fu.Form.submit();
}

//用来添加文件列表的函数
function AddList(rows){
	//根据数组来添加列表
	var FileList = $("idFileList"), oFragment = document.createDocumentFragment();
	//用文档碎片保存列表
	Each(rows, function(cells){
		var row = document.createElement("tr");
		Each(cells, function(o){
			var cell = document.createElement("td");
			if(typeof o == "string"){ cell.innerHTML = o; }else{ cell.appendChild(o); }
			row.appendChild(cell);
		});
		oFragment.appendChild(row);
	})
	//ie的table不支持innerHTML所以这样清空table
	while(FileList.hasChildNodes()){ FileList.removeChild(FileList.firstChild); }
	FileList.appendChild(oFragment);
}


$("idLimit").innerHTML = fu.Limit;

$("idExt").innerHTML = fu.ExtIn.join("，");

$("idBtndel").onclick = function(){ fu.Clear(); }

//在后台通过window.parent来访问主页面的函数
function Finish(msg){ alert(msg); window.location.href=window.location.href;}
</script>
<?php html_footer();?>